package ngames.com.sushitv.action;

import android.app.Activity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.adapter.AnimeIntroAdapter;
import ngames.com.sushitv.adapter.AnimeSingleVideoAdapter;
import ngames.com.sushitv.adapter.VideoRankAdapter;
import ngames.com.sushitv.base.BaseListAction;
import ngames.com.sushitv.core.ModelCallback;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.entity.UpUser;
import ngames.com.sushitv.entity.VideoBase;
import ngames.com.sushitv.entity.VideoDetails;
import ngames.com.sushitv.view.FlowLayout;

/**
 * Created by Jeffrey on 2016/7/27.
 */
public class AnimeSingleAction extends BaseListAction {
    private TextView titleTxt, shareTxt, favTxt, rewardTxt, introTxt, upnameTxt, uploadsTxt, followersTxt, episodeTxt, lastEpisodeTxt;
    private RecyclerView recyclerView1, recyclerView2;
    private ImageView upImg;

    public AnimeSingleAction(Activity activity, View baseView) {
        super(activity, baseView);
    }

    @Override
    public void onCreate() {
        super.onCreate();


//        RecyclerView recyclerView1 = (RecyclerView) mBaseView.findViewById(R.id.recyclerView1);
//        RecyclerView recyclerView2 = (RecyclerView) mBaseView.findViewById(R.id.recyclerView2);
//        final TextView textView = (TextView) mBaseView.findViewById(R.id.intro_text);
//        textView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                textView.setMaxLines(Integer.MAX_VALUE);
//            }
//        });
//
//        ArrayList<ItemVideo> videos = new ArrayList<>();
//
//        List<Integer> integers = new ArrayList<>();
//        integers.add(1);
//        integers.add(1);
//        integers.add(1);
//        integers.add(1);
//        integers.add(1);
//        integers.add(1);
//        integers.add(1);
//        integers.add(1);
//        integers.add(1);
//
//        recyclerView1.setLayoutManager(new GridLayoutManager(getActivity(),7));
//        recyclerView2.setLayoutManager(new LinearLayoutManager(getActivity()));
//
//        //AnimeSingleEPAdapter adapter = new AnimeSingleEPAdapter(getActivity(),integers);
//        AnimeSingleVideoAdapter adapter1 = new AnimeSingleVideoAdapter(getActivity(),videos);
//
//        //recyclerView1.setAdapter(adapter);
//        recyclerView2.setAdapter(adapter1);


        recyclerView1 = (RecyclerView) mBaseView.findViewById(R.id.recyclerView1);
        recyclerView2 = (RecyclerView) mBaseView.findViewById(R.id.recyclerView2);
        final TextView textView = (TextView) mBaseView.findViewById(R.id.intro_text);
        titleTxt = (TextView) mBaseView.findViewById(R.id.title);
        shareTxt = (TextView) mBaseView.findViewById(R.id.text_share);
        favTxt = (TextView) mBaseView.findViewById(R.id.text_fav);
        rewardTxt = (TextView) mBaseView.findViewById(R.id.text_reward);
        introTxt = (TextView) mBaseView.findViewById(R.id.intro_text);
        upnameTxt = (TextView) mBaseView.findViewById(R.id.name_main);
        uploadsTxt = (TextView) mBaseView.findViewById(R.id.uploads_count);
        followersTxt = (TextView) mBaseView.findViewById(R.id.followers_count);
        episodeTxt = (TextView) mBaseView.findViewById(R.id.text_Episode);
        lastEpisodeTxt = (TextView) mBaseView.findViewById(R.id.text_lastEpisode);
        upImg = (ImageView) mBaseView.findViewById(R.id.logo_main);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setMaxLines(Integer.MAX_VALUE);
            }
        });


        //recyclerView1.setLayoutManager(new GridLayoutManager(mActivity, 2));
        recyclerView1.setLayoutManager(new GridLayoutManager(mActivity, 7));

        recyclerView2.setLayoutManager(new LinearLayoutManager(mActivity));


        onRefreshData();
    }

    private void initData() {
        Log.e("animeSingle", "ok" + AppApplication.getmVsId());
        getAppModel().videoDetails(AppApplication.getmVsId(), new ModelCallback<ArrayList<ItemVideo>>() {
            @Override
            public void onBeforeRequest(String urlTag) {

            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> data) {

                setData();
            }

            @Override
            public void onFailure(String errorEvent, String message) {

            }
        });
    }

    private void setData() {
        VideoDetails v = AppApplication.getmVideoDetails();
        VideoBase vb = v.getBase();
        UpUser up = v.getUser();
        titleTxt.setText(vb.getTitle());
        shareTxt.setText(vb.getShare_count());
        favTxt.setText(vb.getFav_count());
        rewardTxt.setText(vb.getReward_count());
        introTxt.setText(vb.getVideo_summary());
        episodeTxt.setText("Episodes " + vb.getEpisode());
        lastEpisodeTxt.setText("Updated to EP " + vb.getLast_episode());

        AnimeIntroAdapter seriesAdapter = new AnimeIntroAdapter(mActivity, v.getSeries());
        recyclerView1.setAdapter(seriesAdapter);
//        upnameTxt.setText(up.getName());
//        uploadsTxt.setText("Uploads: "+up.getUploads());
//        followersTxt.setText("Followers: "+up.getFollows());

//        DisplayImageOptions options = new DisplayImageOptions.Builder()
//                    /*.showStubImage(R.drawable.image_is_loading)            // 设置图片下载期间显示的图片
//                    .showImageForEmptyUri(R.drawable.image_loading_failed)    // 设置图片Uri为空或是错误的时候显示的图片
//                    .showImageOnFail(R.drawable.image_loading_failed)        // 设置图片加载或解码过程中发生错误显示的图片
//                   */
//                .displayer(new RoundedBitmapDisplayer(190))
//                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
//                .cacheOnDisc(true)                            // 设置下载的图片是否缓存在SD卡中
//                .build();
//        String workerImgStr = up.getAvatar();
//        ImageLoader.getInstance().displayImage(workerImgStr, upImg, options);

//        VideoRankAdapter adapter = new VideoRankAdapter(mActivity, v.getRecommends());
//        //FanMadeIntroVideoAdapter adapter = new FanMadeIntroVideoAdapter(mActivity, fanMadeVideos);
        List<String> strings = new ArrayList<>();
        strings.add("Views");
        strings.add("Comments");

        FlowLayout flowLayout = (FlowLayout) mBaseView.findViewById(R.id.flow_layout);
        flowLayout.setIfHide(false);
        flowLayout.setText(strings);

        AnimeSingleVideoAdapter adapter1 = new AnimeSingleVideoAdapter(mActivity, v.getRecommends());
//
        //recyclerView1.setAdapter(adapter);
        recyclerView2.setAdapter(adapter1);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefreshData() {
        initData();
    }

    @Override
    public void loadMore() {
//        videoRank();
    }


}
